package com.jzo2o.health.handler;

import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.health.enums.OrderStatusEnum;
import com.jzo2o.health.model.domain.Orders;
import com.jzo2o.health.model.dto.OrderSnapshotDTO;
import com.jzo2o.health.service.IOrdersService;
import com.jzo2o.statemachine.core.StatusChangeEvent;
import com.jzo2o.statemachine.core.StatusChangeHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * TODO
 *
 * @Author whx
 * @Date 2024/12/17 20:16
 */
@Component("order_cancel")
public class OrderCancelHandler implements StatusChangeHandler<OrderSnapshotDTO> {

    @Resource
    private IOrdersService ordersService;

    @Override
    public void handler(String bizId, StatusChangeEvent statusChangeEventEnum, OrderSnapshotDTO bizSnapshot) {

        boolean resultOrder = ordersService.lambdaUpdate()
                .eq(Orders::getId, bizId)
                .set(Orders::getOrderStatus, statusChangeEventEnum.getTargetStatus())
                .update();
        if(!resultOrder) {
            throw new DBException("更新订单失败");
        }
    }
}
